#!/usr/bin/php
<?php
$vars = parse_ini_file("/var/www/localhost/ini/settings.ini");
$connect = mysql_connect($vars["mysql_host"],$vars["mysql_username"],$vars["mysql_password"]) or die(mysql_error());
$db = mysql_select_db($vars["mysql_database"],$connect);
$sql = "SELECT id,start,end,filename,channel,CONVERT(TIME_TO_SEC(timediff(end,start))/60,signed),wbs from torecord order by start";
$result = mysql_query($sql,$connect) or die (mysql_error());
$current = date("Y-m-d H:i");
while ($row = mysql_fetch_array($result)) {
	//$row[0],$row[1],$row[2],$row[3],$row[4],$row[5]
	$id = $row[0];
	$start = $row[1];
	$end = $row[2];
	$filename = $row[3];
	$channel = $row[4];
	$length = $row[5];
	$wbs = $row[6];	

	$minstart = strtotime($start);
	$minstart = date("Y-m-d H:i",$minstart);

	echo "Current is: $current To Start is $minstart\n";
	
	if ($current == $minstart) {
		//find an open tuner
		$tuner = 0;
		if (!file_exists("/tmp/tuner0")) {
			$tuner=0;
		} else if (!file_exists("/tmp/tuner1")) {
			$tuner=1;			
		} else if (!file_exists("/tmp/tuner2")) {
                        $tuner=2;
		} else if (!file_exists("/tmp/tuner3")) {
                        $tuner=3;
		} else if (!file_exists("/tmp/tuner4")) {
                        $tuner=4;
		} else if (!file_exists("/tmp/tuner5")) {
                        $tuner=5;
		}	
		//start the recording
		//Also set record to 1, so that way we don't mess something up
		$sql ="UPDATE torecord set recording=\"1\" where id=\"$id\"";
		$result2 = mysql_query($sql,$connect);
		if ($wbs > 0) {
			system("(/root/tv_record/record.sh -channel $channel -length $length -tuner $tuner -filename $filename -id $id -wbs $wbs -usenet > /tmp/$filename) > /dev/null &");
		} else {
			system("(/root/tv_record/record.sh -channel $channel -length $length -tuner $tuner -filename $filename -id $id -usenet > /tmp/$filename) > /dev/null &");
		}
		sleep(1);
	}
	
}
//check if we are maybe not recording something we should be.
echo "Checking if we are missing anything...\n";


//$sql = "SELECT id,start,end,filename,channel,CONVERT(TIME_TO_SEC(timediff(end,start))/60,signed) from torecord  where recording=0 and start > now() and end  < now() order by start";
$sql = "SELECT id,start,end,filename,channel,CONVERT(TIME_TO_SEC(timediff(end,start))/60,signed),wbs from torecord  where recording=0 and start < now() and end > now() order by start";
$result = mysql_query($sql,$connect) or die (mysql_error());
$current = date("Y-m-d H:i");
while ($row = mysql_fetch_array($result)) {
	$id = $row[0];
	$start = $row[1];
	$end = $row[2];
	$filename = $row[3];
	$channel = $row[4];
	$length = $row[5];
	$wbs = $row[6];
	$minstart = strtotime($start);
	$minstart = date("Y-m-d H:i",$minstart);
	echo "Current is: $current To Start is $minstart Fixing not started...\n";
	$tuner = 0;
	if (!file_exists("/tmp/tuner0")) {
		$tuner=0;
	} else if (!file_exists("/tmp/tuner1")) {
		$tuner=1;			
	} else if (!file_exists("/tmp/tuner2")) {
                $tuner=2;
	} else if (!file_exists("/tmp/tuner3")) {
                $tuner=3;
	} else if (!file_exists("/tmp/tuner4")) {
        	$tuner=4;
	} else if (!file_exists("/tmp/tuner5")) {
		$tuner=5;
	}	
	//start the recording
        //Also set record to 1, so that way we don't mess something up
        $sql ="UPDATE torecord set recording=\"1\" where id=\"$id\"";
        $result2 = mysql_query($sql,$connect) or die (mysql_error());
	if ($wbs > 0) {
        	system("(/root/tv_record/record.sh -channel $channel -length $length -tuner $tuner -filename $filename -id $id -wbs $wbs -usenet >> /tmp/$filename) > /tmp/output &");
	} else {
		system("(/root/tv_record/record.sh -channel $channel -length $length -tuner $tuner -filename $filename -id $id -usenet >> /tmp/$filename) > /tmp/output &");
	}
        sleep(1);	
}


?>
